<template>
  <div class="app-container">
    <div class="filter-container">
      <el-button-group>
        <el-button :type="filterForm.remove == 1?'success':'default'" @click="filterForm.remove=1;">全部</el-button>
        <el-button :type="filterForm.remove == 3?'success':'default'" @click="filterForm.remove=3">我的机构</el-button>
        <el-button :type="filterForm.remove == 2?'success':'default'" @click="filterForm.remove=2;">回收站</el-button>
      </el-button-group>
    </div>
    <div class="filter-container">
      <el-form ref="filterForm" :model="filterForm" :inline="true" label-position="left" label-width="100px">
        <el-form-item label="关键字" prop="keyword">
          <el-input v-model="filterForm.keyword" placeholder="机构ID/机构标题/结构介绍" style="width: 200px;" class="filter-item" @keyup.enter.native="handleFilter" />
        </el-form-item>
        <el-form-item label="发布时间" prop="publishTime">
          <el-date-picker 
            v-model="filterForm.publishTime"
            value-format="yyyy-MM-dd"
            type="datetimerange"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="产业" prop="industrialID">
            <el-select v-model="filterForm.industrialID" placeholder="请选择">
              <el-option v-for="item in industrialOptions" :key="item.ID" :label="item.title" :value="item.ID" />
            </el-select>
        </el-form-item>
        <el-form-item label="区域" prop="districtID">
            <el-select v-model="filterForm.districtID" placeholder="请选择">
              <el-option v-for="item in districtOptions" :key="item.ID" :label="item.title" :value="item.ID" />
            </el-select>
        </el-form-item>
        <el-form-item label="作者" prop="employee">
          <el-input v-model="filterForm.employee" placeholder="作者ID/作者电话/作者别名" style="width: 200px;" class="filter-item" @keyup.enter.native="handleFilter" />
        </el-form-item>
        <el-form-item label="状态" prop="status">
            <el-select v-model="filterForm.status" placeholder="请选择">
              <el-option label="0" value="0">全部</el-option>
              <el-option label="6" value="6">正常</el-option>
              <el-option label="6" value="5">禁用</el-option>
              <el-option label="1" value="1">草稿</el-option>
            </el-select>
        </el-form-item>
        <el-form-item>
            <el-button v-waves class="filter-item" type="primary" icon="el-icon-search" @click="handleFilter">
              搜索
            </el-button>
            <el-button class="filter-item" style="margin-left: 10px;" type="primary" icon="el-icon-edit" @click="handleCreate">
              新建
            </el-button>
            <el-button class="filter-item" style="margin-left: 10px;" type="danger" icon="el-icon-delete" @click="handleRemove">
              删除
            </el-button>
        </el-form-item>
      </el-form>
    </div>

    <el-table
      :key="tableKey"
      v-loading="listLoading"
      :data="list"
      border
      fit
      highlight-current-row
      style="width: 100%;"
      @sort-change="sortChange"
      @selection-change="handleSelectionChange"
    >
      >
      <el-table-column
        type="selection"
        width="55"
      />
      <el-table-column label="图片" width="100">
        <template slot-scope="{row}">
          <el-avatar shape="square" fit="cover " :src="row.avatar"></el-avatar>
        </template>
      </el-table-column>
      <el-table-column label="标题" min-width="300px">
        <template slot-scope="{row}">
          <span class="link-type" @click="handleChild(row)">{{ row.fullname }}【{{row.title}}】</span>
        </template>
      </el-table-column>
      <el-table-column label="产业" min-width="150px">
        <template slot-scope="{row}">
          <span>{{ row.industrial.title }}</span>
        </template>
      </el-table-column>
      <el-table-column label="隶属机构" min-width="150px">
        <template slot-scope="{row}">
          <span>{{ row.mechanism.title }}</span>
        </template>
      </el-table-column>
      <el-table-column label="状态" class-name="status-col" width="100">
        <template slot-scope="{row}">
          <el-tag :type="row.status">
            {{ row.status.label }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" width="160" class-name="small-padding fixed-width" fixed="right">
        <template slot-scope="{row}">
          <el-button size="mini" type="success" @click="handeUpdate(row)">
            修改
          </el-button>
          <el-button v-if="row.status.value === 5 || row.status.value === 1" size="mini" @click="handleModifyStatus(row,6)">
            启用
          </el-button>
          <el-button v-if="row.status.value === 6" size="mini" type="danger" @click="handleModifyStatus(row,5)">
            禁用
          </el-button>
          <el-button v-if="row.status.value == 1 || row.status.value === 4 || row.status.value == 2 || row.status.value == 5" size="mini" type="danger" @click="handleDelete(row)">
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="filterForm.start" :limit.sync="filterForm.perpage" @pagination="getList" />
  </div>
</template>

<script>
import { getProductionList, changeStatusByProductionID, removeProductionByID } from '@/api/knowledge/production'
import waves from '@/directive/waves' // waves directive
import { } from '@/utils'
import Pagination from '@/components/Pagination' // secondary package based on el-pagination

export default {
  name: 'ComplexTable',
  components: { Pagination },
  directives: { waves },
  filters: {
  },
  data() {
    return {
      listLoading: false,
      tableKey: 0,
      selectedProductionIDs:[],
      filterForm: {
        remove:1,
        start: 1,
        perpage: 10,
        keyword: undefined,
        sort: '+id'
      },
      list: null,
      total: 0,
      industrialOptions:[],
      districtOptions:[],
      productionForm: {

      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    handleCreate(row) {
        console.log(row)
      if(row != undefined){
          this.$router.push({ path: 'production-add', query: { 'parentProductionID': row.ID }})
      }else{
        console.log(this.filterForm)
        if(this.filterForm.productionID != undefined){
            this.$router.push({ path: 'production-add', query: { 'parentProductionID': this.filterForm.productionID }})
        }else{
          this.$router.push({ path: 'production-add' })
        }
      }
    },
    handleChild:function(row){
      this.filterForm.productionID = row.ID;
      this.filterForm.start = 1
      this.getList()
    },
    handeUpdate(item) {
      this.$router.push({ path: 'production-add', query: { 'productionID': item.ID }})
    },
    handleSelectionChange(selectedDocuments) {
      selectedDocuments.forEach((row)=>{
        this.selectedProductionIDs.push(row['ID'])
        console.log(row)
      });
    },
    sortChange(data) {
      const { prop, order } = data
      if (prop === 'id') {
        this.sortByID(order)
      }
    },
    sortByID(order) {
      if (order === 'ascending') {
        this.filterForm.sort = '+id'
      } else {
        this.filterForm.sort = '-id'
      }
      this.handleFilter()
    },
    getList() {
      this.listLoading = true
      getProductionList(this.filterForm).then(response => {
        this.list = response.data.list
        this.total = response.data.total

        // Just to simulate the time of the request
        setTimeout(() => {
          this.listLoading = false
        }, 1.5 * 1000)
      })
    },
    handleFilter() {
      this.filterForm.start = 1
      this.getList()
    },
    handleModifyStatus(row, status) {
      const _this = this
      const formData = {
        'productionID': row.ID,
        'status': status
      }
      changeStatusByProductionID(formData).then(response => {
        if (response.status === 200) {
          this.$message({
            message: '操作Success',
            type: 'success'
          })
          // _this.filterForm.start = 1
          _this.getList()
        } else {
          this.$message({
            message: response.msg,
            type: 'error'
          })
        }
      })
    },
    handleRemove:function(){
        const _this = this
        const formData = {
          'productionID': this.selectedProductionIDs
        }
        removeDocumentationById(formData).then(response => {
          if (response.status === 200) {
            this.$message({
              message: '操作Success',
              type: 'success'
            })
            // _this.filterForm.start = 1
            _this.getList()
            _this.selectedProductionIDs = []
          } else {
            this.$message({
              message: response.msg,
              type: 'error'
            })
          }
        })
    },
    handleDelete(row, index) {
      const _this = this
      const formData = {
        'productionID': row.ID
      }
      removeProductionByID(formData).then(response => {
        if (response.status === 200) {
          this.$message({
            message: '操作Success',
            type: 'success'
          })
          // _this.filterForm.start = 1
          _this.getList()
        } else {
          this.$message({
            message: response.msg,
            type: 'error'
          })
        }
      })
    }
  }
}
</script>
